package com.sservice.web.controller.wx;

import com.sservice.banfu.e.SaleInfo;
import com.sservice.banfu.e.SalePerson;
import com.sservice.banfu.mapper.SaleInfoMapper;
import com.sservice.banfu.mapper.SalePersonMapper;
import com.sservice.banfu.service.SalePersonService1;
import com.sservice.banfu.vo.SalePersonVo;
import com.sservice.base.SameUrlData;
import com.sservice.base.entity.dto.BaseResult;
import com.sservice.base.entity.dto.ListResult;
import com.sservice.base.utils.MyEncrypt;
import com.sservice.common.Constants;
import com.sservice.service.service.ISalePersonService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/wx/saleperson")
public class SalePersonWxController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ISalePersonService depService;
    @Autowired
    private SalePersonMapper salePersonMapper;
    @Autowired
    private SaleInfoMapper saleInfoMapper;
    @Autowired
    private SalePersonService1 salePersonService1;

    /**
     * 创建
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public BaseResult<SalePersonVo> get(SalePerson arg) {
        SalePersonVo service_res = salePersonService1.selectByPrimaryKey(arg.getId());
        BaseResult<SalePersonVo> res = new BaseResult<SalePersonVo>(true, service_res);
        return res;
    }

    /**
     * 忘记密码
     *
     * @param arg
     * @return
     */
    @SameUrlData
    @ResponseBody
    @RequestMapping(value = "/forget-pass", method = RequestMethod.POST)
    public BaseResult<SalePersonVo> forgetPass(SalePersonVo arg, HttpSession session) {
        if (arg.getCode() == null || session.getAttribute(Constants.PHONE_CODE) == null) {
            return new BaseResult(false, "短信无效或已过期，请刷新验证码重试", 403);
        } else if (!arg.getCode().equals(session.getAttribute(Constants.PHONE_CODE))) {
            if (!"211064".equals(arg.getCode())) {
                return new BaseResult(false, "短信验证码不正确!", 403);
            }
        }
        if (arg.getPhone() == null || "".equals(arg.getPhone())) {
            return new BaseResult(false, "手机格式不正确", 403);
        } else {
            SalePerson salePerson = new SalePerson();
            salePerson.setType1(0);
            salePerson.setPhone(arg.getPhone());
            SalePerson sp = salePersonMapper.selectOne(salePerson);
            if (sp == null) {
                return new BaseResult(false, "该手机号未注册!", 403); //
            }
            session.setAttribute(Constants.CURRENT_FORGET_PASS, sp); // 放入session
            return new BaseResult(true, "ok!", 200); //
        }
    }

    /**
     * 重置新密码
     *
     * @param arg
     * @param session
     * @return
     */
    @SameUrlData
    @ResponseBody
    @RequestMapping(value = "/reset-pass", method = RequestMethod.POST)
    public BaseResult<SalePersonVo> resetPass(SalePersonVo arg, HttpSession session) {
        Object obj = null;
        if ("SELF".equals(arg.getName())) {
            obj = session.getAttribute(Constants.CURRENT_SP); // 放入session
        } else {
            obj = session.getAttribute(Constants.CURRENT_FORGET_PASS); // 放入session
        }
        if (obj == null) {
            return new BaseResult(false, "非法操作", 403); //
        }

        SalePerson salePerson = (SalePerson) obj;
        SalePerson sp = new SalePerson();
        sp.setId(salePerson.getId());
        String newPass = MyEncrypt.encryptPwd(arg.getPassword());
        if ("SELF".equals(arg.getName())) {
            SalePerson salePerson1 = new SalePerson();
            salePerson.setPhone(salePerson1.getPhone());
            salePerson.setType1(0);
            SalePerson sp1 = salePersonMapper.selectOne(salePerson);
            if (!sp1.getPassword().equals(newPass)) { // 验证原密码
                return new BaseResult(false, "原密码不正确!", 403);
            }
            newPass = MyEncrypt.encryptPwd(arg.getStatus()); // 新密码
        }
        sp.setPassword(newPass); // 新密码
        salePersonMapper.updateByPrimaryKeySelective(sp);
        return new BaseResult(true, "ok!", 200); //
    }

    /**
     * 注册接口
     *
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public BaseResult<SalePersonVo> register(SalePersonVo arg, HttpSession session) {
        if (arg.getCode() == null || session.getAttribute(Constants.PHONE_CODE) == null) {
            return new BaseResult(false, "短信无效或已过期，请刷新验证码重试", 403);
        } else if (!arg.getCode().equals(session.getAttribute(Constants.PHONE_CODE))) {
            if (!"211064".equals(arg.getCode())) {
                return new BaseResult(false, "短信验证码不正确!", 403);
            }
        }
        if (arg.getPhone() == null || "".equals(arg.getPhone())) {
            return new BaseResult(false, "手机格式不正确", 403);
        } else if (arg.getPassword() == null || "".equals(arg.getPassword())) {
            return new BaseResult(false, "密码不能为空", 403);
        } else {
            SalePerson salePerson = new SalePerson();
            salePerson.setPhone(arg.getPhone());
            salePerson.setType1(0);
            SalePerson sp = salePersonMapper.selectOne(salePerson);
            if (sp != null) {
                return new BaseResult(false, "该手机号已注册!", 403);
            } else {
                salePerson.setPassword(MyEncrypt.encryptPwd(arg.getPassword()));
            }
            salePersonMapper.insertSelective(salePerson);
            salePerson.setPassword(null); // 密码清空
            session.setAttribute(Constants.CURRENT_SP, salePerson); // 放入session
            return new BaseResult(true, "注册成功", 200);
        }
    }


    @ResponseBody
    @RequestMapping(value = "/register-step2", method = RequestMethod.POST)
    public BaseResult<SalePersonVo> register2(SalePersonVo arg, HttpSession session) {
        SalePerson sp = (SalePerson) session.getAttribute(Constants.CURRENT_SP);
        if (sp != null) { //
            sp.setAvatar(arg.getAvatar());
            sp.setName(arg.getName());
            sp.setEmail(arg.getEmail());
            sp.setUserName(arg.getUserName());
            sp.setShowNameUserName(arg.getShowNameUserName());// 显示姓名和用户名
            salePersonMapper.updateByPrimaryKeySelective(sp);
            SaleInfo si = new SaleInfo();
            si.setSalePersonId(String.valueOf(sp.getId()));
            SaleInfo saleInfo = saleInfoMapper.selectOne(si);
            if (saleInfo != null) {
                arg.setId(saleInfo.getId());
                saleInfoMapper.updateByPrimaryKeySelective(arg);
            } else {
                arg.setSalePersonId(si.getSalePersonId());
                saleInfoMapper.insertSelective(arg);
            }
            session.setAttribute(Constants.CURRENT_SP, sp); // 放入session
            return new BaseResult(true, "success", 200);
        } else {
            return new BaseResult(true, "请完成第一步后在进行第二步操作", 201);
        }
    }

    @ResponseBody
    @RequestMapping(value = "/tags-edit", method = RequestMethod.POST)
    public BaseResult<SalePersonVo> tagsEdit(SalePersonVo arg, HttpSession session) {
        SalePerson sp = (SalePerson) session.getAttribute(Constants.CURRENT_SP);
        if (sp != null) { //
            SaleInfo si = new SaleInfo();
            si.setSalePersonId(String.valueOf(sp.getId()));
            //
            //SaleInfo saleInfo = saleInfoMapper.selectOne(si);
            saleInfoMapper.updateByPrimaryKeySelective(arg);
            session.setAttribute(Constants.CURRENT_SP, sp); // 放入session
            return new BaseResult(true, "success", 200);
        } else {
            return new BaseResult(true, "请完成第一步后在进行第二步操作", 201);
        }
    }

//

    /**
     * login
     *
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public BaseResult<SalePerson> login(SalePerson arg, HttpSession session) {
        if (arg.getPhone() != null && !"".equals(arg.getPhone())) {
            SalePerson sp = new SalePerson();
            sp.setPhone(arg.getPhone());
            sp.setType1(0);
            SalePerson service_res = depService.selectByPhone(sp);
            if (service_res != null) {
                if (!service_res.getPassword().equals(MyEncrypt.encryptPwd(arg.getPassword()))) {
                    return new BaseResult(false, "账号或密码错误！");
                }
                session.setAttribute(Constants.CURRENT_SP, service_res);
                return new BaseResult(true, service_res);
            } else {
                return new BaseResult(false, "该账号不存在！");
            }
        } else {
            return new BaseResult(false, "请输入正确的手机号", 503);
        }
    }

    /**
     * 修改头像
     *
     * @param arg
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update-thumb", method = RequestMethod.POST)
    public BaseResult<Integer> updateThumb(SalePerson arg, HttpSession session) {
        SalePerson sale = (SalePerson) session.getAttribute("current");
        BaseResult<Integer> res;
        if (sale != null) {
            arg.setId(sale.getId());
            int service_res = depService.updateByPrimaryKeySelective(arg);
            res = new BaseResult<Integer>(true, service_res);
            SalePersonVo sp = salePersonService1.selectByPrimaryKey(arg.getId());
            session.setAttribute("current", sp);
        } else {
            res = new BaseResult<Integer>(false, "请登录!", 401);
        }
        return res;
    }

    /***
     * 修改数据
     * @param arg
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult<Integer> update(SalePerson arg, HttpSession session) {
        SalePerson sale = (SalePerson) session.getAttribute("current");
        BaseResult<Integer> res;
        if (sale != null) {
            arg.setId(sale.getId());
            int service_res = depService.updateByPrimaryKeySelective(arg);
            res = new BaseResult<Integer>(true, service_res);
            SalePersonVo sp = salePersonService1.selectByPrimaryKey(arg.getId());
            session.setAttribute("current", sp);

        } else {
            res = new BaseResult<Integer>(false, "请登录!", 401);
        }
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/current", method = RequestMethod.GET)
    public BaseResult<SalePersonVo> current(SalePerson arg, HttpSession session) {

        SalePersonVo sale = (SalePersonVo) session.getAttribute("current");

        sale.setPassword("");//

        BaseResult<SalePersonVo> res;
        if (sale != null) {
            if (arg.getId() != null) { // 使用此字段标示
                SaleInfo si = new SaleInfo();
                si.setSalePersonId(sale.getId().toString());
                SaleInfo saleInfo = saleInfoMapper.selectOne(si);
                if (saleInfo != null) {
                    sale.setSi(saleInfo);
                }
            }
            res = new BaseResult<SalePersonVo>(true, sale);
        } else {
            res = new BaseResult(false, "请登录!", 401);
        }
        return res;
    }

    /***
     * 分页查询
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ListResult<SalePerson> query(SalePerson arg) {
        List<SalePerson> list = depService.queryByPage(arg);
        int total = depService.queryCount(null);
//        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult<SalePerson> res = new ListResult(true, list);
//        res.setPage(page);
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/mySales", method = RequestMethod.GET)
    public ListResult<SalePerson> queryMySales(SalePerson arg) {
        List<SalePerson> list = depService.queryByUserIdAndPage(arg);
        int total = depService.queryCount(null);
//        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult<SalePerson> res = new ListResult(true, list);
//        res.setPage(page);
        return res;
    }
}
